from rest_framework.generics import ListAPIView, ListCreateAPIView

from apps.meiduo_admin.serializers.users import UserListViewSerializer, UserAddListViewSerializer
from apps.meiduo_admin.utils import UserDefinePagination
from apps.users.models import User


class UserListView(ListCreateAPIView):
    # 根据要求返回不同的查询集
    def get_queryset(self):
        keyword = self.request.query_params.get("keyword")
        if keyword:
            return User.objects.filter(username__contains=keyword)
        return User.objects.all()

    # 根据不同的请求方式返回不同的序化器
    def get_serializer_class(self):
        if self.request.method == "GET":
            return UserListViewSerializer
        elif self.request.method == "POST":
            return UserAddListViewSerializer

    pagination_class = UserDefinePagination
